package com.dcbeida.adapter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.dcbeida.bean.Drug;
import com.dcbeida.fragment.StatisticsFragment;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class HomePageFragmentAdapter extends FragmentStatePagerAdapter {


    List<Drug> mDrugList = new ArrayList<>();

    public HomePageFragmentAdapter(FragmentManager fm, List<Drug> drugList) {
        super(fm);
        this.mDrugList = drugList;
    }

    public HomePageFragmentAdapter(FragmentManager fm) {
        super(fm);

    }

    public void addAll(Collection<? extends Drug> c) {
        mDrugList.clear();
        mDrugList.addAll(c);
        notifyDataSetChanged();
    }

    public void clear() {
        mDrugList.clear();
        notifyDataSetChanged();
    }


    public void setmDrugList(List<Drug> mDrugList) {
        this.mDrugList = mDrugList;
    }

    @Override
    public Fragment getItem(int position) {
        Fragment statisticsFragment = new StatisticsFragment();
        Bundle bundle = new Bundle();
        if (!mDrugList.isEmpty()) {
            bundle.putInt("drugId", mDrugList.get(position).getDrugid());
            bundle.putString("name",mDrugList.get(position).getDrugname());
        }
        statisticsFragment.setArguments(bundle);
        return statisticsFragment;
    }

    @Override
    public int getCount() {
        return mDrugList.isEmpty() ? 0 : mDrugList.size();
    }
}
